from typing import Optional
from langchain_core.pydantic_v1 import BaseModel, Field
from ChatGLM_new import tongyi_llm
# 定义 Joke 类
class Joke(BaseModel):
    """Joke to tell user."""

    setup: str = Field(description="The setup of the joke")
    punchline: str = Field(description="The punchline to the joke")
    rating: Optional[int] = Field(description="How funny the joke is, from 1 to 10")

# 使用 with_structured_output() 方法
structured_llm = tongyi_llm.with_structured_output(Joke)

# 调用模型生成笑话
print(structured_llm.invoke("Tell me a joke about cats"))
